package cn.csust.net2.manager.server.util;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import cn.csust.net2.manager.client.ux.ColumnInfo;
import cn.csust.net2.manager.shared.util.Constant;

public class Util {
	public static Field getPrimary(Field[] fields) {

		for (Field f : fields) {
			f.setAccessible(true);
			if (f.isAnnotationPresent(ColumnInfo.class)) {
				if (f.getAnnotation(ColumnInfo.class).isPrimary())
					return f;
			}

		}
		return null;
	}

	/**
	 * 首个字母改成小写
	 * 
	 * @param s
	 * @return
	 */
	public static String firstToLow(String s) {

		String result = new String(Character.toLowerCase(s.charAt(0))
				+ s.substring(1));
		return result;
	}
	
	public static ApplicationContext getApplicationContext() {
		ApplicationContext act=new FileSystemXmlApplicationContext("/WEB-INF/Spring-Application.xml"); 
		return act;
	}
	/**
	 * 判断当前时间是否在给定的时间段内
	 * @param start
	 * @param last
	 * @return
	 */
	public static boolean juge(Date start,int last){
		Calendar cal = Calendar.getInstance();
		Date now = cal.getTime();
		int c = now.compareTo(start);
		switch(c){
		case 0:
			return true;
		case 1:
			cal.add(Calendar.DAY_OF_MONTH, last);
			Date over = cal.getTime();
			if(now.compareTo(over)!=1){
				return true;
			}else{
				return false;
			}
		case -1:
			return false;
		}
		
		
		return false;
	}
	/**
	 * 判断给定时间是否是今年的日期,不是将给定时间推定到本年
	 * @param year
	 * @return
	 */
	public static Date judgeYearNow(Date year){
		Calendar now = Calendar.getInstance();
		Calendar cal = Calendar.getInstance();
		cal.setTime(year);
		if(now.get(Calendar.YEAR)==cal.get(Calendar.YEAR))
			return null;
		else{
			cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
			return cal.getTime();
		}
	}
	/**
	 * 加载当前日期的学期
	 * @return
	 */
	public static String getNowTerm(Date entryDate ){
		Calendar c = Calendar.getInstance();
		Date now = c.getTime();
		c.setTime(entryDate);
		c.set(Calendar.MONTH, 9);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.MONTH,5);
		Date start2 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start3 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start4 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start5 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start6 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start7 = c.getTime();
		c.add(Calendar.MONTH,5);
		Date start8 = c.getTime();
		if(now.after(entryDate)&&now.before(start2))
			return Constant.GRDE_1;
		else if(now.after(start2)&&now.before(start3))
			return Constant.GRDE_2;
		else if(now.after(start3)&&now.before(start4))
			return Constant.GRDE_3;
		else if(now.after(start4)&&now.before(start5))
			return Constant.GRDE_4;
		else if(now.after(start5)&&now.before(start6))
			return Constant.GRDE_5;
		else if(now.after(start6)&&now.before(start7))
			return Constant.GRDE_6;
		else if(now.after(start7)&&now.before(start8))
			return Constant.GRDE_7;
		else if(now.after(start8))
			return Constant.GRDE_8;
		else
			return Constant.GRDE_1;
	}
	public static Date getDate(String s ){
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return sd.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
